﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 表示必须小于给定的值, 示例:
/// <code>
/// [VMustLessThan(100)]
/// public double Score { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustLessThanAttribute(object value, string errorMessage = null) : Attribute, IValidateAction
{
    public object Value { get; } = value;
    public string ErrorMessage { get; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateExtensions.MustLessThanInternal(ctx, Value, ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则必须小于给定的值, 示例:
/// <code>
/// [VMustLessThanIfNotNull(100)]
/// public double? Score { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustLessThanIfNotNullAttribute(object value, string errorMessage = null) : Attribute, IValidateAction
{
    public object Value { get; } = value;
    public string ErrorMessage { get; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateExtensions.MustLessThanInternal(ctx, Value, ErrorMessage);
    }
}
